import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: { // 私有属性
    token: '',
    // 反序列化
    userInfo: JSON.parse(localStorage.getItem("userInfo")),
    cart: JSON.parse(localStorage.getItem("cart")),
    semesterId: localStorage.getItem("semesterId")
  },
  mutations: {// setXXX
    SET_TOKEN: (state, token) => {
      state.token = token
      localStorage.setItem("token",token)
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo
      localStorage.setItem("userInfo",JSON.stringify(userInfo))
    },
    SET_CART: (state, cart) => {
      state.cart = cart
      localStorage.setItem("cart", JSON.stringify(cart))
    },
    SET_SEMESTERID: (state, semesterId) => {
      state.semesterId = semesterId
      localStorage.setItem("semesterId", semesterId)
    },
    REMOVE_INFO: (state) => {
      state.token = ''
      state.userInfo = {}
      state.cart = []
      state.semesterId = null
      localStorage.setItem("token",'')
      localStorage.setItem("userInfo",JSON.stringify(''))
      localStorage.setItem("cart", JSON.stringify(''))
      localStorage.setItem("semesterId", null)
    },
  },
  modules: {
    app,
    settings,
  },
  getters
})

export default store
